package pkg

import (
	"fmt"
	"github.com/golang-jwt/jwt/v5"
	"log"
	"time"
)

func GetJwtToken(userId string) (string, error) {
	claims := make(jwt.MapClaims)
	iat := time.Now().Unix()
	claims["exp"] = iat + 86400
	claims["iat"] = iat
	claims["userId"] = userId
	token := jwt.New(jwt.SigningMethodHS256)
	token.Claims = claims
	secretKey := "123456"
	return token.SignedString([]byte(secretKey))
}
func ParseToken(tokenString string) (string, error) {
	token, err := jwt.Parse(tokenString, func(token *jwt.Token) (any, error) {
		// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
		secretKey := "123456"
		return []byte(secretKey), nil
	}, jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}))
	if err != nil {
		log.Fatal(err)
		return "", err
	}

	if claims, ok := token.Claims.(jwt.MapClaims); ok {
		fmt.Println(claims["userId"], claims["nbf"])
	} else {
		fmt.Println(err)
	}
	return "", nil
}
